//
//  CMDBUserCenterProductionCell.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/4.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let tagsID = "tagsID"

class CMDBUserCenterProductionCell: CustomBaseCell {
    
    var separateView : UIView        = UIView.getInstance()

    var coverImageView : UIImageView = UIImageView.getInstance()
    
    var nameLabel : UILabel          = UILabel.getInstance()
    
    var timeLabel : UILabel          = UILabel.getInstance()
    
    var boxOfficeLabel : UILabel     = UILabel.getInstance()
    
    var tagsView : UIView            = UIView.getInstance()
    
    var attributesBtns : [UIButton]  = [UIButton]()
    
    var array : [CMDBTagModel] = [CMDBTagModel]()
    
    lazy var collectionView : UICollectionView = {[weak self] in
        
        //1.创建layout
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width: 50, height: 20)
        
        layout.minimumLineSpacing = 10
        
        layout.minimumInteritemSpacing = 0.5
        
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 5)
        
        layout.scrollDirection = .Horizontal
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        
        collectionView.showsVerticalScrollIndicator = false
        
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.bounces = false
        
        collectionView.delegate = self
        
        collectionView.dataSource = self
        
        collectionView.clipsToBounds = false
        
        collectionView.pagingEnabled = true
        
        collectionView.registerClass(CMDBTagCollectionCell.self, forCellWithReuseIdentifier: tagsID)
        
        return collectionView
        
        }()

    
    override func setupCell() {
        super.setupCell()
        
        backgroundColor = UIColor.whiteColor()
    }
    
    override func cellBuildSubViews() {
        super.cellBuildSubViews()
        
        separateView    = UIView(frame: CGRect(x: 0, y: 0, width: CMDB_Screen_Width, height: 10))
        
        separateView.backgroundColor = UIColor.Hex(0xf2f2f2)
        
        contentView.addSubview(separateView)
        
        coverImageView  = UIImageView(frame: CGRect(x: KleftMargin, y: CGRectGetMaxY(separateView.frame) + KleftMargin, width: 180 * CMDB_Rate, height: 240 * CMDB_Rate))
        
        coverImageView.image = UIImage(named: "相册背景图")
        
        contentView.addSubview(coverImageView)
        
        nameLabel   = UILabel.getInstance()
        
        nameLabel.textColor = word87Color
        
        nameLabel.font = CMDB_18_FONT
        
        nameLabel.text = "鬼吹灯之云南虫谷"
        
        var size = nameLabel.text?.suitToSize(CGSize(width: 999, height: 999), fontSize: 18, lineSpacing: 1)
        
        nameLabel.frame = CGRect(x: CGRectGetMaxX(coverImageView.frame) + 10, y: CGRectGetMaxY(separateView.frame) + 30, width: size!.width, height: size!.height)
        
        contentView.addSubview(nameLabel)
        
        timeLabel.text = "年份:"
        
        timeLabel.font = CMDB_10_FONT
        
        timeLabel.textColor = word54Color
        
        timeLabel.text = timeLabel.text!.stringByAppendingString("2016年")
        
        size = timeLabel.text!.suitToSize(MaxSize, fontSize: 10, lineSpacing: 1)
        
        contentView.addSubview(timeLabel)
        
        timeLabel.snp_makeConstraints { (make) in
            make.right.equalTo(-30 * CMDB_Rate);
            make.top.equalTo(22 + 10);
            make.width.equalTo(size!.width);
            make.height.equalTo(size!.height);
        }
        
        boxOfficeLabel.text = "票房:"
        
        boxOfficeLabel.textColor = word54Color
        
        boxOfficeLabel.font = CMDB_10_FONT
        
        boxOfficeLabel.text = boxOfficeLabel.text!.stringByAppendingString("1.6亿")
        
        size = boxOfficeLabel.text!.suitToSize(MaxSize, fontSize: 10, lineSpacing: 1)
        
        contentView.addSubview(boxOfficeLabel)
        
        boxOfficeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_left);
            make.top.equalTo(timeLabel.snp_bottom).offset(5);
            make.width.equalTo(size!.width);
            make.height.equalTo(size!.height);
        }
//        buildAttributesView()
        
        buildAttrData()
        
        buildAtttibuteColelctionView()
        
    
    }
    
    override func loadContent() {
        
    }

}

//MARK:- 设置标签
extension CMDBUserCenterProductionCell{
    
    func buildAttrData(){
        
        let model01 = CMDBTagModel(tagName: "导演")
        
        let model02 = CMDBTagModel(tagName: "演员")
        
        let model03 = CMDBTagModel(tagName: "编剧")
        
        array.append(model01)
        array.append(model02)
        array.append(model03)
        
    }
    
    //设置标签
    func buildAttributesView(){
        
        tagsView = UIView(frame: CGRect(x: CGRectGetMinX(nameLabel.frame) , y: 66 + 20, width: CMDB_Screen_Width - CGRectGetMinX(nameLabel.frame), height: 93 - 20))
        
        let singleView = UIView.CreateLine(CGRect(x: 0, y: -15, width: tagsView.width - KleftMargin, height: 0.5), lineColor: word12Color)
        
        tagsView.addSubview(singleView)
        
        for index in 0..<4 {
            
            let attriteTitle = "标签"
            
            let attributeBtn = UIButton(title: attriteTitle, borderColor: UIColor.Hex(0xff6d00), titleSizeFont: CMDB_12_FONT, isShowBorder: true)
            
            attributeBtn.setTitleColor(word87Color, forState: .Normal)
            
            let size = attriteTitle.suitToSize(MaxSize, fontSize: 12, lineSpacing: 1)
            
            let x : CGFloat = index > 0 ? KleftMargin + CGRectGetMaxX(attributesBtns[index - 1].frame): 0.0
            
            attributeBtn.frame = CGRect(x: x , y: 0, width: size.width + 20, height: size.height + 5)
            
            attributesBtns.append(attributeBtn)
            
            tagsView.addSubview(attributeBtn)
        }
        
        contentView.addSubview(tagsView)
    }
    
    func buildAtttibuteColelctionView(){
        
        contentView.addSubview(collectionView)
        
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(lonePressMoving(_:)))
        
        collectionView.addGestureRecognizer(longPress)
        
        collectionView.backgroundColor = UIColor.whiteColor()
        
        collectionView.frame =  CGRect(x: CGRectGetMinX(nameLabel.frame) , y: 66 + 20, width: CMDB_Screen_Width - CGRectGetMinX(nameLabel.frame), height: 30)
        
    }
    
}

//MARK:- collectionViewDataSource
extension CMDBUserCenterProductionCell : UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(tagsID, forIndexPath: indexPath) as! CMDBTagCollectionCell
        
        cell.tagModel = array[indexPath.item]
        
        cell.backgroundColor = UIColor.whiteColor()
        
        return cell
    }
}


//MARK:- collectionViewDelegate
extension CMDBUserCenterProductionCell : UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    
    func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
        return true
    }
}

//MARK:- action methods
extension CMDBUserCenterProductionCell{
    
    @objc func lonePressMoving(sender : UILongPressGestureRecognizer){
        
        switch sender.state {
        case .Began:
            
            let selectedIndexPath = collectionView.indexPathForItemAtPoint(sender.locationInView(collectionView))
            
            let cell = collectionView.cellForItemAtIndexPath(selectedIndexPath!) as! CMDBTagCollectionCell
            
            cell.deleteBtn.hidden = false
            
            cell.deleteBtn.tag = selectedIndexPath!.item
            
            cell.deleteBtn.addTarget(self, action: #selector(deleteBtnClcik(_:)), forControlEvents: .TouchUpInside)
            
            break
        case .Changed:
            //          collectionView.updateInteractiveMovementTargetPosition(sender.locationInView(sender.view))
            
            break
            
        case .Ended:
            collectionView.endInteractiveMovement()
            
            break
            
        default:
            collectionView.cancelInteractiveMovement()
            
            print("default")
        }
        
    }
    
    @objc func deleteBtnClcik(sender : UIButton){
        
        let index = sender.tag
        
        array.removeAtIndex(index)
        
        let collectionIndexPath = NSIndexPath(forItem: index, inSection: 0)
        
        UIView.animateWithDuration(0.3) {
            
            self.collectionView.deleteItemsAtIndexPaths([collectionIndexPath])
            
        }
        
        printLog(indexPath)
        
        if array.count == 0 {
            mDelegate?.onItemClickType!("DeleteCell", data: nil, indexPath:self.indexPath)
        }
    }
    
}
